package kickboxing.controller;

import java.sql.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import kickboxing.model.Newsy;
import kickboxing.util.HibernateUtil;

public class NewsyManager extends HibernateUtil{
        
		/**
		 * Metoda dodajaca nowego newsa
		 * 
		 * @param news
		 * @return
		 */
     	public Newsy dodajNowyNews(Newsy news){
                Session session = HibernateUtil.getSessionFactory().getCurrentSession();
                session.beginTransaction();
                
                Date data = new Date(new java.util.Date().getTime());
                
                news.setData(data);
                session.save(news);
                session.getTransaction().commit();
                session.close();
                return news;
        }
        
        /**
         * Metoda zwracajaca wszystkie newsy w kolejnosci od daty dodania
         * @return lista newsow
         */
		public List<Newsy> wysietlWszystkieNewsy(){
                Session session = HibernateUtil.getSessionFactory().openSession();
                session.beginTransaction();
                
                List<Newsy> lista = null;
                try{	
                	
                        Query q = session.createQuery("from Newsy ORDER BY(data) DESC");
                        lista = q.list();
                        
                } catch (HibernateException e) {
                        e.printStackTrace();
                        session.getTransaction().rollback();
                
                }
                session.getTransaction().commit();
                session.close();
                return lista;
                
        }
		
		/**
		 * Metoda usuwajaca newsa po wskazanym id
		 * @param id - id newsa ktorego chcesz usunac
		 * @return
		 */
		public Newsy usunNewsa(int id){
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			Newsy news = (Newsy) session.load(Newsy.class, id);
			System.out.println(news.getTemat());
			session.delete(news);
			session.getTransaction().commit();
			session.close();
			return news;
			
		}
		
		/**
		 * Metoda sluzaca do edytowania newsow
		 * @param news
		 * @return
		 */
		public Newsy edytujNewsa(Newsy news){
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			System.out.println("Temat: " + news.getTemat());
			news.setTemat(news.getTemat());
			news.setTresc(news.getTresc());
			news.setData(news.getData());
			session.update(news);
			session.getTransaction().commit();
			session.close();
			return news;
			
		}
		
		/**
		 * Metoda usuwajaca znaczniki html
		 * @param htmlString
		 * @return
		 */
		public static String removeHTML(String htmlString)
	    {
	          // Remove HTML tag from java String    
	        String noHTMLString = htmlString.replaceAll("\\<.*?\\>", "");

	        // Remove Carriage return from java String
	        noHTMLString = noHTMLString.replaceAll("\r", "<br/>");

	        // Remove New line from java string and replace html break
	        noHTMLString = noHTMLString.replaceAll("\n", " ");
	      
	        noHTMLString = noHTMLString.replaceAll("\'", "&#39;");
	        noHTMLString = noHTMLString.replaceAll("\"", "&quot;");
	        return noHTMLString;
	    } 

}